#include<iostream>
#include<fstream>
using namespace std;
const short LEN = 3000;
void multiply(short lh[], short rh, short result[]);
void add(short lh[], short rh[], short result[]);
void factorial(short n, short result[]);
void my_memset(short array[], short num, short len);
int main(int argc, char * argv[])
{
    short n;
    cout << "please input n: " ;
    cin >> n;
    short result[LEN];
    factorial(n, result);
    short begin = 0;
    for(short i = 0; i < LEN; i++)
    {
        if(result[i] != 0)
        {
            begin = i;
            break;
        }
    }
    for(short i = begin; i < LEN; i++)
    {
        cout << result[i];
    }
    cout<<endl;
    return 0;
}

void add(short lh[], short rh[], short result[])
{
    short carry = 0;
    short bit_sum = 0;
    for(short i = LEN - 1; i >= 0; i--)
    {
        bit_sum = lh[i] + rh[i] + carry;
        carry = bit_sum / 10;
        result[i] = bit_sum % 10;
    }
}


void multiply(short lh[], short n, short result[])
{

    short bit_num = 0;
    short count = 0;
    short * add_l = new short[LEN];
    short * add_r = new short[LEN];
    my_memset(add_l, 0, LEN);
    while(n)
    {
        bit_num = n % 10;
        n = n / 10;
        short carry = 0;
        short tmp = 0;
        
        for(short i = LEN - 1, j = LEN - 1 - count; j >= 0; i--, j--)
        {
            tmp = lh[i] * bit_num + carry;
            carry = tmp / 10;
            add_r[j] = tmp % 10;
        }
        add(add_l, add_r, add_l);
        count++;
        my_memset(add_r, 0, LEN);
    }
    for(short i = 0;i < LEN; i++)
    {
        result[i] = add_l[i];
    }
    delete[] add_l;
    delete[] add_r;
}




void factorial(short n, short result[])
{
    short * lh;
    lh = new short[LEN];
    my_memset(lh, 0, LEN);
    lh[LEN-1] = 1;
    for(short i = 1; i <= n; i++)
    {
        short * temp_result = new short[LEN];
        multiply(lh, i, temp_result);
        delete[] lh;
        lh = temp_result;
    }
    for(short i = 0; i <= LEN; i++)
    {
        result[i] = lh[i];
    }
}

void my_memset(short array[], short num, short len)
{
    for(short i = 0; i < len; i++)
    {
        array[i] = num;
    }
}


